#include <stdlib.h>
#include <stdio.h>
#include "DynamicArray.h"

#define false 0
#define true 1
// 数组初始化
int ArrayInit(DMArray *array)
{ // 申请堆上的空间 size*   数据类型的大小
    array->dp = (ElementType *)malloc(array->size * sizeof(ElementType));
    // 是否申请成功的判断
    if (array->dp == NULL)
    {
        return false;
    }

    return true;
}

void FreeArray(DMArray *array)
{
    if (array->dp != NULL)
    {
        free(array->dp);
        array->dp = NULL;
    }
}
int ReallocArray(DMArray *array) // 封闭式接口
{
    ElementType *temp = array->dp;
    array->dp = (ElementType *)malloc(sizeof(ElementType) * array->size * 2);
    if (array->dp == NULL)
    {
        printf("ReallocArray Error !\n");
        return false;
    }
    for (int i = 0; i < array->len; i++)
    {
        array->dp[i] = temp[i];
    }
    for (int i = 0; i < array->len; i++)
    {
        array->dp[i] = temp[i];
    }
    array->size = array->size * 2;
    free(temp);
    return true;
}
int InsertArray(DMArray *array, ElementType element)
{
    if (array->len == array->size)
    {
        if (ReallocArray(array) == false)
        {
            printf("can not contain more elements!\n");
            return false;
        }
    }
    array->dp[array->len] = element;
    array->len++;
    return true;
}
